<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>贪食蛇</title>
    <style>
        * {
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }

        .main {
            width: 360px;
            height: 420px;
            background-color: #bfa;
            margin: 50px auto;
            border: 10px #000 solid;
            border-radius: 20px;
        }

        #stage {
            width: 304px;
            height: 304px;
            border: 2px #000 solid;
            margin: 20px auto;
            position: relative;
        }

        #snake>div {
            width: 10px;
            height: 10px;
            background-color: #000;
            position: absolute;
            border: 1px solid #bfa;

        }

        #food {
            width: 10px;
            height: 10px;
            position: absolute;
            top: 100px;
            left: 120px;
            display: flex;
            flex-flow: wrap;
        }

        #food>div {
            width: 5px;
            height: 5px;
            background-color: #000;
            transform: rotate(45deg);
        }

        #info {
            width: 304px;
            margin: 30px auto;
            display: flex;
            justify-content: space-between;
            font: bold 20px courier;
        }
    </style>
</head>

<body>
    <div class="main">
        <div id="stage">
            <div id="snake">
                <div></div>

            </div>
            <div id="food">
                <div></div>
                <div></div>
                <div></div>
                <div></div>
            </div>
        </div>
        <div id="info">
            <div>SCORE:<span id="score">0</span></div>
            <div>LEVEL:<span id="level">1</span></div>
        </div>

    </div>

    <script>
        // 获取蛇的容器
        const snake = document.getElementById("snake")

        // 获取蛇身体部分
        const snakes = snake.getElementsByTagName("div")

        
        // 获取食物
        const food = document.getElementById("food")
        
        
        /* 
            食物的坐标在0~290之间
        */
        function changeFood(){
            const x = Math.floor(Math.random() * 30) * 10
            const y = Math.floor(Math.random() * 30) * 10

            // 设置食物坐标
            food.style.left = x + "px"
            food.style.top = y + "px"
        }
        
        

        // 定义一个变量存储蛇的方向
        let dir

        let active = true

        /* 绑定按键事件keydown
                - 键盘事件只绑定给可以获取的焦点或者是document
        */

        // 设置按键数组
        const keyArr = ["ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight", "p"]

        const reObj = {

            ArrowUp: "ArrowDown",
            ArrowDown: "ArrowUp",
            ArrowLeft: "ArrowRight",
            ArrowRight: "ArrowLeft"
        }

        document.addEventListener("keydown", function (event) {
            if (active && keyArr.includes(event.key)) {
                if(snakes.length < 2 || reObj[dir] != event.key){
                    // 设置方向
                    dir = event.key
                    // 
                    active = false
                }
            }


        })

        setTimeout(function move() {

            // 获取蛇头部分
            const head = snakes[0]

            // 获取蛇头的坐标
            let y = head.offsetTop
            let x = head.offsetLeft

            switch (dir) {
                case "ArrowUp":
                    // 向上移动蛇
                    y -= 10
                    break
                case "ArrowDown":
                    // 向下移动蛇
                    y += 10
                    break
                case "ArrowLeft":
                    // 向左移动蛇
                    x -= 10
                    break
                case "ArrowRight":
                    // 向右移动蛇
                    x += 10
                    break
            }

            // 检查蛇是否吃到食物
            if (head.offsetTop === food.offsetTop && head.offsetLeft === food.offsetLeft){
                // 1.改变食物的位置
                changeFood()

                //2.增加蛇的身体
                snake.insertAdjacentHTML("beforeend","<div/>") 

                // 3.增加分数
                score++
                scoreSpan.textContent = score

                // 4.等级升级
                if(score % 5 === 0){
                    level++
                    levelSpan.textContent = level + 1
                    if(level > 14){
                        alert("您已经成为了蛇王！！")
                        return
                    }
                }
            }
            

            /* 
                判断游戏是否结束
                    1.撞墙
                    2.撞自己
            */

            // 判断是否撞墙
            if( x < 0 || x > 290 || y < 0 || y > 290){
                alert("撞墙了！游戏结束！！！")
                return
            }

            // 判断是否撞到自己
            for( let i=0; i<snakes.length - 1; i++){
                if(y === snakes[i].offsetTop && x === snakes[i].offsetLeft){
                    alert("撞到自己了！！游戏结束！！")
                    return
                }
            }
            
            // 获取尾巴的位置
            const tail = snakes[snakes.length - 1]
            // 移动蛇的位置
            tail.style.top = y + "px"
            tail.style.left = x + "px"

            // 把蛇尾改成蛇头
            snake.insertAdjacentElement("afterbegin",tail)

            active = true

            setTimeout(move, 300 - level*20)
        }, 300);

        // 获取score和level的span
        const scoreSpan = document.getElementById("score")
        const levelSpan = document.getElementById("level")

        // 创建变量储存分数和等级
        let score = 0
        let level = 0

    </script>
</body>

</html>